package com.subfg.common.util.auth;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class IdGenerateorSnowflake {
    private Logger log = LoggerFactory.getLogger(IdGenerateorSnowflake.class);
    @Value("${workerId}")
    private long workerId;
    @Value("${datacenterId}")
    private long datacenterId;

    private Snowflake snowflake = IdUtil.createSnowflake( workerId, datacenterId);

    public synchronized long snowflakeId() {
        return snowflake.nextId();
    }

    private String nextId(Integer length) {
        long id  = snowflakeId();
        return String.format("%10d", id);
    }

    public synchronized String snowflakeAccount() {
        return nextId(8);
    }
}
